package com.example.teacher.Controller;

import cn.hutool.json.JSONUtil;
import com.example.common.domain.Result;
import com.example.common.domain.pojo.Course;
import com.example.common.domain.pojo.CourseOutline;
import com.example.common.domain.pojo.TypeHandler.JsonToObject;
import com.example.common.utils.RoleContext;
import com.example.common.utils.UUIDUtil;
import com.example.common.utils.UserContext;
import com.example.teacher.Service.CourseOutlineService;
import com.example.teacher.Service.CourseService;
import com.example.teacher.Service.SectionService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RefreshScope//时时刷新配置信息
@Slf4j  //日志  log.info("登录日志测试：");
@RequestMapping("/teachers")
@RequiredArgsConstructor
public class TeacherTestController {

    @Autowired
    private CourseService courseService;
    @Autowired
    private CourseOutlineService outlineService;
    @Autowired
    private SectionService sectionService;
    @Autowired
    private JsonToObject json;


    @GetMapping("/test")
    public String test() {
        String userContext = UserContext.getUser();
        String role = RoleContext.getRole();
        System.out.println(userContext);
        System.out.println(role);
        log.info("登录日志测试：");
        return "success";
    }

    @GetMapping("/getCourseById2/{id}")
    Result<Course> getCourseById(@PathVariable String id) {
        log.info("/getCourseById2/{id课程ID：");
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    @PostMapping("/TestPost")
    public Result<Course> TestPost(@RequestBody Course course) {
        course.setId(UUIDUtil.generate(9));
        course.setName("软件工程");
        log.info("课程信息：{}", course);
        return Result.success(course);
    }


    @ApiOperation("获取课程大纲")
    @GetMapping("/getCourseOutline2/{courseId}")
    public Result<CourseOutline> getCourseOutline(
            @ApiParam(value = "课程ID", required = true)
            @PathVariable String courseId) {
        log.info("获取课程大纲，课程ID：{}", courseId);
        try {
            CourseOutline outline = outlineService.teacherGetChapter(courseId);
            String teachingContentJson = JSONUtil.toJsonStr(outline.getTeachingElements());
            Map<String, List<String>> outlineMapT = json.parseToMapList(teachingContentJson);
            System.out.println(outlineMapT);
            Map<String, List<String>> outlineMap = new ObjectMapper().readValue(
                    teachingContentJson,
                    new TypeReference<Map<String, List<String>>>() {
                    }
            );
            outline.setTeachingContent(outlineMap);
            System.out.println(outlineMap);
            return Result.success(outline);
        } catch (Exception e) {
            log.error("获取课程大纲失败：", e);
            return Result.error("获取课程大纲失败：" + e.getMessage());
        }
    }


}
